package action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Util;
import bean.catalog.Catalog;

import dm.CatalogDM;

import gate.ActionHandler;

public class ActionCatalog implements ActionHandler{

	@Override
	public void handleAction(HttpServletRequest request, HttpServletResponse response) {

		try{
			String action = request.getParameter("action");
			
			
			String url = request.getParameter("url");
			CatalogDM dm = new CatalogDM();
			
			try{
				if(action.equals("getCatalog")){
					
					String id = request.getParameter("categoryId");
					String name = request.getParameter("categoryName");
					
					ArrayList<Catalog> catalog = dm.getCatalogList(id, name);
					request.setAttribute("result", catalog);
					
				}else if (action.equals("addCatalog")) {
					
					String id = request.getParameter("parentId");
					String name = request.getParameter("name");
					
					dm.insertCatalog(id, name);
					
				}else if (action.equals("updateCatalog")) {
					
					String id = request.getParameter("categoryId");
					String name = request.getParameter("categoryName");
					
					dm.updateCatalog(id, name);
					
				}else if (action.equals("delCatalog")) {
					
					String id = request.getParameter("categoryId");
					
					dm.delCatalog(id);
				}
			} catch (Exception e) {
				
				request.setAttribute("error", e.getMessage());
				// TODO: handle exception
			}
			
			request.getRequestDispatcher(url).forward(request, response);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
